<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\admin\command\Project;

use app\common\constant\Constant;
use app\common\manager\MatchManager;
use app\common\model\Match;
use app\common\model\Member;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;

class MatchCancel extends Command
{
    protected $model = null;

    protected function configure()
    {
        $this->setName('matchCancel')
            ->setDescription('task');
        //要执行的controller必须一样，不适用模糊查询
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            $page = 1;
            $pageSize = 10;
            while (true){
                $start = ($page - 1) * $pageSize;
                $matchInfo = Match::where('sign_end_date', '<',time())->where('status', Match::STATUS_INIT)->where('delete',\app\admin\model\Match::NO_DELETED)->field('m_id,least_count,sign_end_date,join_count')->limit($start, $pageSize)->select();
                if (empty($matchInfo)){
                    exit;
                }
                foreach ($matchInfo as $item){
                    $leastCount = $item['least_count'];
                    $joinCount = $item['join_count'];
                    $mId = $item['m_id'];
                    $signEndDate = $item['sign_end_date'];
                    $strSignEndDate = date('Y-m-d H:i:s', $signEndDate);
                    if ($joinCount < $leastCount){
                        $result = MatchManager::cancelMatchBySystem($mId);
                        print_r($result);
                        if ($result['code'] == MatchManager::$successCode){
                            $output->writeln("取消比赛成功：m_id:{$mId},sign_end_date:{$strSignEndDate},join_count:{$joinCount},least_count:{$leastCount}");
                        }else{
                            $output->writeln("取消比赛失败：m_id:{$mId},sign_end_date:{$strSignEndDate},join_count:{$joinCount},least_count:{$leastCount},error_msg:{$result['message']}");
                        }
                    }
                }
                $page++;
            }
        }catch (Exception $e){
            echo $e->getMessage();
        }

    }

}
